package com.william.yun.yunliwuli.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.william.yun.yunliwuli.api.request.gateway.RebootGatewayRequest;
import com.william.yun.yunliwuli.service.CoreService;
import com.william.yun.yunliwuli.service.GatewayService;

import java.util.ArrayList;
import java.util.List;

public class GatewayServiceImpl extends CoreService implements GatewayService {
    /**
     * 加入网关
     *
     * @param body
     * @param token
     * @return
     */
    @Override
    public JSONObject insertGateway(JSONObject body, String token) {
        return postJsonResult(body, token, GATEWAY_URL);
    }

    /**
     * 修改网关
     *
     * @param body
     * @param token
     * @return
     */
    @Override
    public JSONObject updateGateway(JSONObject body, String token) {
        return putJsonResult(body, token, GATEWAY_URL);
    }

    /**
     * 移除网关
     *
     * @param uuid
     * @param token
     * @return
     */
    @Override
    public JSONObject removeGateway(String uuid, String token) {
        return deleteJsonResult(null, token, GATEWAY_URL + "/" + uuid);

    }

    /**
     * 重启网关
     *
     * @param body
     * @param token
     * @return
     */
    @Override
    public JSONObject rebootGateway(JSONObject body, String token) {
        return postJsonResult(new JSONObject(), token, jsonToString(body, REBOOT_GATEWAY_URL));

    }

    /**
     * 价签绑定网关
     *
     * @param body
     * @param token
     * @return
     */
    @Override
    public JSONObject postPubLabelBinding(JSONObject body, String token) {
        JSONArray macs = body.getJSONArray("macs");
        List<String> macList = new ArrayList<>();
        for (int i = 0; i < macs.size(); i++) {
            String mac = macs.getJSONObject(i).getString("mac");
            macList.add(mac);
        }
        String jsonArray = JSONArray.toJSONString(macList);
        return postJsonResult(JSONArray.parseArray(jsonArray), token, PUB_LABEL_BINDING_URL.concat("?storeUuid=").concat(body.getString("storeUuid")));
    }

    public static void main(String[] args) {
        List<String> macList = new ArrayList<>();
        macList.add("11111");
        macList.add("222222");
        macList.add("333333");
        macList.add("4444444");
        macList.add("555555");
        String jsonArray = JSONArray.toJSONString(macList);
        System.out.println(jsonArray);
    }
    /**
     * 查询网关
     * @param body
     * @param token
     * @return
     */
    @Override
    public JSONObject getGateway(JSONObject body, String token) {
        ;
        return getJsonResult(body, token, jsonToString(body, GATEWAY_URL));
    }

    /**
     * 查询网关具体信息
     *
     * @param body
     * @param token
     * @return
     */
    @Override
    public JSONObject postGatewayMessageByMac(JSONObject body, String token) {
        return postJsonResult(body, token, GETGATEWAYMESSAGEBYMAC_URL);
    }
}
